package cn.lhn.ridersclub.controller;

import cn.lhn.ridersclub.dto.Result;
import cn.lhn.ridersclub.entity.Paper;
import cn.lhn.ridersclub.mapper.PaperMapper;
import cn.lhn.ridersclub.service.AccountFollowService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/flush")
public class FlushController {

    private final PaperMapper paperMapper;

    private final AccountFollowService accountFollowService;

    public static Boolean paperFlag;

    public static final Map<Long, Boolean> clubMap = new Hashtable<>();

    public static final Map<Long, Boolean> accountMap = new Hashtable<>();

    public FlushController(PaperMapper paperMapper, AccountFollowService accountFollowService) {
        this.paperMapper = paperMapper;
        this.accountFollowService = accountFollowService;
    }

    @GetMapping("/hasNewPaper")
    public Result<Boolean> hasNewPaper(Long count) {
        Boolean result = paperFlag;
        paperFlag = false;
        return Result.success(result);
    }

    @GetMapping("/hasNewClub")
    public Result<Boolean> hasNewClub(Long id) {
        Boolean result = clubMap.get(id);
        clubMap.put(id, false);
        return Result.success(result);
    }

    @GetMapping("/hasNewAccount")
    public Result<Boolean> hasNewAccount(Long id) {
        Boolean result = accountMap.get(id);
        accountMap.put(id, false);
        return Result.success(result);
    }

    @GetMapping("/hasNewPaperByClub")
    public Result<Boolean> hasNewPaperByClub(Long clubId, String paperIdStr, Integer size) {

        QueryWrapper<Paper> qw = new QueryWrapper<>();
        qw.eq("club_id", clubId);
        List<Paper> list = paperMapper.selectList(qw);

        if (list.size() != size) {
            return Result.success("查询成功", true);
        }

        String[] split = paperIdStr.split("-");

        List<Paper> paperList = new ArrayList<>();
        for (String s : split) {
            try {
                Paper paper = new Paper();
                paper.setId(Long.parseLong(s));
                paperList.add(paper);
            }
            catch (Exception ignored) {
            }
        }

        for (Paper paper : list) {
            if (!paperList.contains(paper)) {
                return Result.success("查询成功", true);
            }
        }

        for (Paper paper : paperList) {
            if (!list.contains(paper)) {
                return Result.success("查询成功", true);
            }
        }

        return Result.success("查询成功", false);
    }

    @GetMapping("/hasNewMyCount")
    public Result<Boolean> hasNewMyCount(Long id, Long dtCount, Long followCount, Long fansCount) {

        QueryWrapper<Paper> qw = new QueryWrapper<>();
        qw.eq("account_id", id);
        List<Paper> paperList = paperMapper.selectList(qw);
        if (paperList.size() != dtCount) {
            return Result.success(true);
        }

        Long fc = accountFollowService.getFollowCount(id);
        if (!fc.equals(followCount)) {
            return Result.success(true);
        }

        Long fsc = accountFollowService.getFansCount(id);
        if (!fsc.equals(fansCount)) {
            return Result.success(true);
        }

        return Result.success(false);
    }

    @GetMapping("/hasNewDT")
    public Result<Boolean> hasNewDT(Long id, String dtIdStr, Integer size) {

        QueryWrapper<Paper> qw = new QueryWrapper<>();
        qw.eq("account_id", id);
        List<Paper> list = paperMapper.selectList(qw);

        if (list.size() != size) {
            return Result.success(true);
        }

        String[] split = dtIdStr.split("-");

        List<Paper> paperList = new ArrayList<>();
        for (String s : split) {
            try {
                Paper paper = new Paper();
                paper.setId(Long.parseLong(s));
                paperList.add(paper);
            }
            catch (Exception ignored) {
            }
        }

        for (Paper paper : list) {
            if (!paperList.contains(paper)) {
                return Result.success("查询成功", true);
            }
        }

        for (Paper paper : paperList) {
            if (!list.contains(paper)) {
                return Result.success("查询成功", true);
            }
        }

        return Result.success("查询成功", false);
    }
}
